package com.zycfc.zsf.boot.exception.handler;

import javax.servlet.http.*;
import com.zycfc.zsf.boot.exception.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.*;
import com.zycfc.zsf.boot.util.json.*;
import com.zycfc.zsf.boot.exception.type.*;
import java.util.*;
import org.springframework.validation.*;
import org.slf4j.*;

@ControllerAdvice
public class GlobalExceptionHandler
{
    private static final Logger log;
    private final ZsfExceptionHandler handler;
    
    public GlobalExceptionHandler(final ZsfExceptionHandler handler) {
        this.handler = handler;
    }
    
    @ExceptionHandler({ Throwable.class })
    public void handleException(final HttpServletRequest request, final HttpServletResponse response, final Throwable ex) {
        try {
            final ExceptionInfo res = this.handler.resolveException(ex);
            this.handler.sendErrorMsg(request, response, res);
        }
        catch (Throwable e) {
            GlobalExceptionHandler.log.error("GlobalExceptionHandler---" + e.getMessage(), e);
            GlobalExceptionHandler.log.error("GlobalExceptionHandler====" + ex.getMessage(), ex);
        }
    }
    
    @ExceptionHandler({ MethodArgumentNotValidException.class })
    public void handleMethodArgumentNotValidException(final HttpServletRequest request, final HttpServletResponse response, final MethodArgumentNotValidException ex) {
        try {
            final BindingResult bindingResult = ex.getBindingResult();
            final StringBuilder errorMesssage = new StringBuilder("\u8bf7\u6c42\u6570\u636e\u6821\u9a8c\u5931\u8d25:");
            for (final FieldError fieldError : bindingResult.getFieldErrors()) {
                GlobalExceptionHandler.log.info(JsonUtils.objectToJson((Object)fieldError));
                errorMesssage.append(fieldError.getDefaultMessage()).append(";");
            }
            final ExceptionInfo exception = new ExceptionInfo(ExceptionCode.DATA_VALID.getValue(), errorMesssage.toString());
            this.handler.sendErrorMsg(request, response, exception);
        }
        catch (Throwable e) {
            GlobalExceptionHandler.log.error("GlobalExceptionHandler---" + e.getMessage(), e);
        }
    }
    
    @ExceptionHandler({ BindException.class })
    public void handleBindException(final HttpServletRequest request, final HttpServletResponse response, final BindException ex) {
        try {
            final FieldError fieldError = ex.getFieldError();
            final StringBuilder sb = new StringBuilder();
            sb.append(fieldError.getField()).append("=[").append(fieldError.getRejectedValue()).append("]").append(fieldError.getDefaultMessage());
            final ExceptionInfo exception = new ExceptionInfo(ExceptionCode.DATA_VALID.getValue(), sb.toString());
            this.handler.sendErrorMsg(request, response, exception);
        }
        catch (Throwable e) {
            GlobalExceptionHandler.log.error("GlobalExceptionHandler---" + e.getMessage(), e);
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)GlobalExceptionHandler.class);
    }
}
